<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;" :max-height="tableHeight">
      <el-table-column label="序号" type="index" align="center"/>
      <el-table-column label="图片" align="center">
        <template slot-scope="{row}">
          <img v-if="row.image" @click="handleOpenImage(row)" :src="row.image" min-width="70" height="70">
        </template>
      </el-table-column>
      <el-table-column label="供需标题" prop="name" align="center" />
      <el-table-column label="规格" prop="specs" align="center" />
      <el-table-column label="价格(元)" prop="topPrice" align="center" />
      <el-table-column label="联系人" prop="connectName" align="center" />
      <el-table-column label="联系电话" prop="connectPhone" align="center" />
      <el-table-column label="供需类别" prop="type" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.type == 1" type="primary">供应信息</el-tag>
          <el-tag v-else type="success">采购信息</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="type" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.status == 1" type="warning">待审核</el-tag>
          <el-tag v-else-if="row.status == 2" type="success">审核通过</el-tag>
          <el-tag v-else type="danger">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="success"
            @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button size="mini" type="danger"
            @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

    <el-image-viewer  v-if="imageVisible" :on-close="closeViewer" :initial-index="0" :url-list="[this.goodImage]" />
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { tradePageByQo, saveTrade, updateTrade, deleteTrade } from '@/api/trade'
import NumInput from '@/components/NumInput'
import updateTableMixin from '@/mixin/updateTable'
import Tinymce from '@/components/Tinymce'
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'

export default {
  name: 'tradeHall',
  directives: { waves },
  props: {},
  components: {
    Pagination,
    NumInput,
    Tinymce,
    ElImageViewer
  },
  mixins: [updateTableMixin],
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        pageNo: 1,
        pageSize: 20
      },
      imageVisible: false,
      goodImage: ''
    }
  },
  computed: {
    ...mapGetters(["userInfo"])
  },
  watch: {
    $route: {
      handler: function(route) {
        const t = route.query.t;
        if (t) {
          this.getList()
        }
      },
      immediate: true
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      tradePageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
        this.updateTableHeight()
      }).catch(error => {
        console.log(error);
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({
    		path: "/tradeHall/edit",
    		query: {
    		}
    	});
    },
    handleEdit(row) {
      this.$router.push({
    		path: "/tradeHall/edit",
    		query: {
    			paramId: row.id
    		}
    	});
    },
    handleDelete(row) {
      this.$confirm('是否继续删除？').then(() => {
        deleteTrade({ ids: row.id }).then(() => {
          this.$message({
              message: '删除成功！',
              type: 'success'
            })
          this.getList()
        })
      })
    },
    handleOpenImage(row) {
      this.goodImage = row.image
      this.imageVisible = true
    },
    closeViewer() {
      this.imageVisible = false
    },
  },
  created() {
    this.getList()
  },
  mounted() {
    
  },
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
}
</script>

<style lang='scss' scoped>
  
</style>